-
Notifications
You must be signed in to change notification settings - Fork 78
Add sample weight support to change loss aggregation #316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
As this affects all networks and is a conversation starter at this early stage, I did not do comprehensive tests to confirm that everything works as intended. When we revamp the integration tests for the generative networks in the near future, it should be no problem to cover usage with and without sample weights. |
|
Codecov ReportAttention: Patch coverage is
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
... and 11 files with indirect coverage changes 🚀 New features to boost your workflow:
|
LarsKue
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keras.Metric has support for sample weights. I feel like we should leverage this rather than trying to reinvent the wheel here. @han-ol can you check if there is a feasible way to implement your desired functionality using keras.Metric?
|
True, sample weights are also supported by keras already, but I think we still have to do some changes to fully support them. As far as I can tell, the proposed changes are not reimplementing anything that When fitting with a PyDataset, keras accepts sample weights as a third tuple item (x,y,sample_weight). We should mimic keras behavior regarding metrics insofar as to only pass weights to metrics in the self.weighted_metrics attribute. For reference: https://keras.io/api/models/model_training_apis/#compile-method @LarsKue, do you have a direction in mind to further exploit the preexisting keras implementation of sample_weight? |
|
@han-ol Thank you for clarifying. I think I see the idea behind the changes now. Since you mentioned that the changes are only a conversation starter for now, could you illustrate some use cases (w.r.t. user code) and perhaps also let us know when you think these changes are production-ready? @stefanradev93 What do you think about the current state of this PR? |
|
I think it's a nice to have option, especially in terms of potential future adaptations. |
|
I made some experiments with weighting already, so I can definitely show it in use if you are interested. However, I personally cannot provide an example where weighting gives a tangible benefit over other approaches yet. Having the infrastructure already and agreeing on UI here sooner rather than later is good, IMO, because this is straightforward to include now, and can then find its way into the docs, tests, etc. at a time when we are (re)writing old and new docs anyways. Abstractly speaking, this sort of sample weights is useful
Regarding timeline:
Another todo for the functionality is
Please let me know if I should add tests prior to merging, or if it makes more sense to add tests during or after refactor of the pre-existing network and integration tests. |
|
I can confirm these are highly relevant usecases for weighting and we will need them sooner rather than later. So I would be very happy to see this PR being merged soon! |
|
@LarsKue @paul-buerkner Should we integrate this functionality? |
|
yes I think so. I think it could be useful among others for Philipps project
Stefan Radev ***@***.***> schrieb am Mi., 12. März 2025,
16:22:
… @LarsKue <https://github.com/LarsKue> @paul-buerkner
<https://github.com/paul-buerkner> Should we integrate this functionality?
—
Reply to this email directly, view it on GitHub
<#316 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADCW2AADNMD5UMJAVRMMSWL2UA7KTAVCNFSM6AAAAABXCEOOHOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDOMJYGA3DOMBWGE>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
[image: stefanradev93]*stefanradev93* left a comment
(bayesflow-org/bayesflow#316)
<#316 (comment)>
@LarsKue <https://github.com/LarsKue> @paul-buerkner
<https://github.com/paul-buerkner> Should we integrate this functionality?
—
Reply to this email directly, view it on GitHub
<#316 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADCW2AADNMD5UMJAVRMMSWL2UA7KTAVCNFSM6AAAAABXCEOOHOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDOMJYGA3DOMBWGE>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
|
Ok, let's merge dev into this branch and I will accept the merge. |
|
Merging done. Quick question: Is "sample_weights" the best name for it? We could also just use "weights" right? Or would this be ambigous? |
|
@paul-buerkner I think both would be fine. |
|
Let's use singular then. Because sample_weight will always be only a single variable, right (data dim = 1)? Not multiple variables as is the case for inference_variables etc. @han-ol if you agree, would you mind changing that? |
|
Yes, agreed. I changed the names "sample_weights" -> "sample_weight" everywhere and also changed the default adapter to expect just one string that is renamed to "sample_weight" (rather than a Sequence of multiple, that is concatenated into "sample_weights"). I added tests as well and support for point inference networks, so the PR is ready to undergo review at this point. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot reviewed 13 out of 13 changed files in this pull request and generated 1 comment.
Adresses #314.
I'll add a first commit to aid discussion.
sample_weightsis a new optional adapter output and is propagated through the compute metrics methods of ContinuousApproximator, InferenceNetwork(s) including FlowMatching, FreeFormFlow and CouplingFlow.